package com.zys.hotel_management.controller;


import com.zys.hotel_management.common.Response;
import com.zys.hotel_management.entity.Room;
import com.zys.hotel_management.service.RoomService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/rooms")
public class RoomController {
    @Resource
    private RoomService roomService;

    @GetMapping
    public Response<List<Room>> getAllRooms() {
        List<Room> rooms = roomService.getAllRooms();
        return Response.success(rooms);
    }

    @GetMapping("/{id}")
    public Response<Room> getRoomById(@PathVariable Integer id) {
        Room room = roomService.getRoomById(id);
        if (room != null) {
            return Response.success(room);
        } else {
            return Response.fail(404, "未找到对应房间信息");
        }
    }

    @PostMapping
    public Response<Void> addRoom(@RequestBody Room room) {
        try {
            room.setStatus(room.getStatus().trim());
            roomService.addRoom(room);
            return Response.success(null);
        } catch (Exception e) {
            return Response.fail(500, "添加房间失败: " + e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Response<Void> updateRoom(@PathVariable Integer id, @RequestBody Room room) {
        try {
            room.setRoomId(id);
            roomService.updateRoom(room);
            return Response.success(null);
        } catch (Exception e) {
            return Response.fail(500, "更新房间信息失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Response<Void> deleteRoom(@PathVariable Integer id) {
        try {
            roomService.deleteRoom(id);
            return Response.success(null);
        } catch (Exception e) {
            return Response.fail(500, "删除房间失败: " + e.getMessage());
        }
    }

    @GetMapping("/available")
    public Response<List<Room>> getAvailableRooms(@RequestParam(required = false) String roomType) {
        List<Room> availableRooms = roomService.getAvailableRooms(roomType);
        return Response.success(availableRooms);
    }
}